class Solution {
    public boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();

        while (n != 1) {
            int sum = 0;
            while (n > 0) {
                sum += Math.pow(n % 10, 2);
                n /= 10;
            }
            if (!set.contains(sum))
                set.add(sum);
            else
                return false;
            n = sum;
        }
        return true;
    }
}
